import {query,saveOrUpdate,findAll,deleteById,queryProductCascadeCategory} from '@/api/product.js'
import { Notification } from 'element-ui';
export default{
  namespaced:true,
  state:{
    totalData:{},
    TotalAllDate:[],
    deleteData:[],
    queryProductCascadeCategoryData:{},
  },
  getters:{
    total:state=>state.totalData.total,
    productData:state=>state.totalData.list,
    queryProductCascadeCategoryDataList:state=>state.queryProductCascadeCategoryData.list,
  },
  mutations:{
    changeTotalDate(state,data){
      state.totalData=data;
    },
    changeTotalAllDate(state,data){
      state.TotalAllDate=data;
    },
    changedeleteData(state,data){
      state.deleteData = data;
    },
    changequeryProductCascadeCategoryData(state,data){
      state.queryProductCascadeCategoryData=data;
    }
  },
  actions:{
    /**
     * 查询产品信息
     * @param {*} param0 
     * @param {*} payload 
     */
    query({commit},payload){
      query(payload).then(res=>{
        commit('changeTotalDate',res.data);
      })
    },
    /**
     * 新增或者修改产品信息
     */
     async saveOrUpdate({dispatch},payload){
      //接受数据,发送请求,请求成功提示用户保存成功,处理page,分发findAll动作
      let res = await saveOrUpdate(payload.form);
      if(res.status===200){
        Notification({
          type:'success',
          title:"成功",
          message:"保存成功"
        });
        //查找数据
        dispatch("query",payload.queryParams);        
      }else{
        Notification({
          type:'error',
          title:"失败",
          message:"保存失败"
        })
      }
    },

    /**
     * 查找所有产品数据
     */
     findAll({commit},payload){
      findAll(payload).then(res=>{
        commit('changeTotalAllDate',res.data);
      })
    },
    /**
     * 通过id删除
     */
    async deleteById({commit},payload){
      let res = await deleteById(payload);
      commit('changedeleteData',res.data);
    },
    /**
     * 产品详情
     */
    async queryProductCascadeCategory({commit},payload){
      let res = await queryProductCascadeCategory(payload);
      commit('changequeryProductCascadeCategoryData',res.data);
    }
  }
}